{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input ../input/4colsY2new.txt\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import math\n",
    "import warnings\n",
    "import pandas as pd # type: ignore\n",
    "\n",
    "def calc_angle(filename):\n",
    "    cwd = os.getcwd()\n",
    "    input_dir = os.path.join(cwd, 'input/')\n",
    "    output_dir = os.path.join(cwd, 'output/')\n",
    "\n",
    "    if os.path.exists('./input/'):\n",
    "      input_dir = './input/'\n",
    "      output_dir = './output/'\n",
    "    elif os.path.exists('../input/'):\n",
    "      input_dir = '../input/'\n",
    "      output_dir = '../output/'\n",
    "    else:\n",
    "      print('input_dir not found!')\n",
    "    \n",
    "    intermediate_dir = output_dir + 'intermediate/'\n",
    "\n",
    "    input_file = os.path.join(input_dir, filename)\n",
    "    print('input', input_file)\n",
    "    \n",
    "    df_in = pd.read_csv(input_file, sep=r'\\s+') # Read TXT file\n",
    "    df_out = pd.DataFrame()\n",
    "    df_out['X分量']      = df_in.iloc[:,0]\n",
    "    df_out['Y分量']      = df_in.iloc[:,1]\n",
    "    df_out['Z分量']      = df_in.iloc[:,2]\n",
    "    df_out['未补磁总场']  = df_in.iloc[:,3]\n",
    "    df_out['航向角']      = df_out.apply(lambda row: math.atan2(row['Y分量'], row['X分量'])*180/math.pi,axis=1)\n",
    "    \n",
    "    #output_file = os.getcwd()/output_name\n",
    "    basename = os.path.splitext(filename)[0]\n",
    "    angle_file = intermediate_dir + basename +'_angle.csv'\n",
    "    df_out.to_csv(angle_file, float_format='%3.2f', sep='\\t', index=False)\n",
    "\n",
    "filename = '4colsY2new.txt'\n",
    "calc_angle(filename)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
